package ch.bfh.swos.learnaid.rest.gcm;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;

public class GCMSender {

    private static final String SENDER_ID = "AIzaSyBmlP-nS99mvc9qyz70DVQkZBYDDoDxkzw";

    private static final String DROID_BIONIC = "APA91bE4M7jxtRB4bP-rSZguo-W-4hNgIWcJ6s2IVA6_4Bh-puxivWcbd2xEsIRJEiCpgXc_bbCQbDponZxxvrq4VFn95CczqP8cpBxP9ymrdx1BhxtSbtuxzYUqcY5Jqj06VfeIswZ__Ujih7eLV3lSlu5UTp2MxA";

    private List<String> androidTargets = new ArrayList<String>();

    private Sender sender;
    private Message message;

    public GCMSender() {
        androidTargets.add(DROID_BIONIC);
        sender = new Sender(SENDER_ID);
    }

    public void sendMessage(String text) {
        message = new Message.Builder().collapseKey("collapseKey").timeToLive(30).delayWhileIdle(true)
                .addData("message", text).build();
        try {
            // use this for multicast messages. The second parameter
            // of sender.send() will need to be an array of register ids.
            MulticastResult result = sender.send(message, androidTargets, 1);

            if (result.getResults() != null) {
                int canonicalRegId = result.getCanonicalIds();
                if (canonicalRegId != 0) {

                }
            } else {
                int error = result.getFailure();
                System.out.println("Broadcast failure: " + error);
            }

        } catch (Exception e) {
            System.out.println("Wer hets denkt es geit nid! "+ e.getMessage());
            e.printStackTrace();
        }
    }
}
